package cas.ihep.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DFSClient;

import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;

/**
 * Created by gesturewei on 17-3-24.
 */
public class HdfsSystem implements java.io.Closeable{
    DFSClient mClient;
    Configuration mConfig;
    static final InetAddress LOCALHOST=getLocalHost();
    static final String LOCALHOST_ADDRESS=LOCALHOST.getHostAddress();

    private static final InetAddress getLocalHost(){
        try{
            return InetAddress.getLocalHost();
        }catch(UnknownHostException e){
            e.printStackTrace();
            return null;
        }
    }

    public HdfsSystem(String namenode, Configuration conf) throws URISyntaxException, IOException {
        URI namenodeuri=new URI(namenode);
        mClient=new DFSClient(namenodeuri,conf);
        mConfig=conf;
    }

    public HdfsFile open(String path)throws IOException{
        return new HdfsFile(this,path);
    }

    public HdfsFile open(Path path) throws IOException{
        return new HdfsFile(this,path);
    }

    public void close()throws IOException{
        mClient.close();
    }
}
